#include "usermodifyrole.h"
#include "ui_usermodifyrole.h"
#include <QListView>
#include "authmanager.h"
#include "userlogger.h"

UserModifyRole::UserModifyRole(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::UserModifyRole)
{
    ui->setupUi(this);
    bdf = BllDataFlow::getInstance();
    sqlHelper = SqlHelper::getInstance();

    //Signals and Slots
    connect(bdf, &BllDataFlow::sendModifyRoleUserSignal, this, &UserModifyRole::setUser);
    connect(bdf, &BllDataFlow::showAllRolesSignal, this, &UserModifyRole::showAllRoles);
    connect(bdf, &BllDataFlow::retranslateUiSignal, this, &UserModifyRole::retranslateUiSlot);

    initWidget();
}

UserModifyRole::~UserModifyRole()
{
    delete ui;
}

void UserModifyRole::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.fillRect(this->rect(), QColor(255, 255, 255, 0));
}

void UserModifyRole::on_btnClose_clicked()
{
    this->hide();
}

void UserModifyRole::retranslateUiSlot()
{
    ui->retranslateUi(this);
}

void UserModifyRole::on_btnConfirm_clicked()
{
    Role role;
    role.name = ui->cbxRole->currentText();
    int permission = sqlHelper->selectRolePermission(role);
    role.permission = permission;

    if (user.role == role.name)
    {
        this->hide();
        return;
    }

    EttaError ret = sqlHelper->updateUserRole(user, role);
    if (ret == EttaError::OK)
    {
        UserLogger::log(QString("%1: %2, %3: %4 -> %5").arg(tr("Modify User")).arg(user.name).arg(tr("Role")).arg(user.role).arg(role.name));
        bdf->showAllUsers();
        this->hide();
    }
    else
    {
        UserLogger::error(QString(tr("Failed to Modify User %1")).arg(user.name));
        QString result = (ret == EttaError::OK) ? tr("Success") : tr("Failed");
        MsgBox::information(this, tr("Modify User"), QString(tr("%1 to Modify User")).arg(result));
    }
}

void UserModifyRole::on_btnCancel_clicked()
{
    this->hide();
}

void UserModifyRole::setUser(User user)
{
    this->user = user;

    for (int i = 0; i < ui->cbxRole->count(); ++i)
    {// 查找 item 项
        if (ui->cbxRole->itemText(i) == user.role)
        {
            ui->cbxRole->setCurrentIndex(i);
            return;
        }
    }

    Q_ASSERT(false);
    ui->cbxRole->setCurrentText("");
}

void UserModifyRole::showAllRoles()
{
    ui->cbxRole->clear();

    QList<Role> info = sqlHelper->selectAllRoles();
    for (int r = 0; r < info.size(); r++)
    {
        if (!AuthManager::instance()->isManufacturer(info[r].name) &&
            !AuthManager::instance()->isAdministrator(info[r].name))
        {
            ui->cbxRole->addItem(info[r].name);
        }
    }
 }

void UserModifyRole::initWidget()
{
    setAttribute(Qt::WA_TranslucentBackground, true);
    QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect();
    shadow->setColor(QColor(0,0,0,150));
    shadow->setOffset(1, 1);
    shadow->setBlurRadius(16);
    ui->widget_2->setGraphicsEffect(shadow);
    ui->cbxRole->setView(new QListView());
    ui->cbxRole->view()->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    showAllRoles();
}
